package cn.com.chec.en.util.images;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import cn.com.chec.en.util.PropertiesUtil;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageScaleUtils {
	private int srcImageWidth;
	private int srcImageHeight;
	private int destImageWidth;
	private int destImageHeight;
	private Image image;
	private String destImage;

	public ImageScaleUtils(String srcImage) {
		this(new File(srcImage));
	}

	public ImageScaleUtils(File srcImage) {
		try {
			String prefix = FilenameUtils.removeExtension(srcImage
					.getCanonicalPath());
			String suffix = FilenameUtils.getExtension(srcImage
					.getCanonicalPath());

			this.destImageWidth = (StringUtils.isNotBlank(PropertiesUtil
					.getValue("width")) ? Integer.valueOf(
					PropertiesUtil.getValue("width")).intValue() : 600);
			this.destImageHeight = (StringUtils.isNotBlank(PropertiesUtil
					.getValue("height")) ? Integer.valueOf(
					PropertiesUtil.getValue("height")).intValue() : 400);

			this.destImage = prefix.concat("_new").concat(".").concat(suffix);
			this.image = ImageIO.read(srcImage);
			this.srcImageWidth = this.image.getWidth(null);
			this.srcImageHeight = this.image.getHeight(null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void resize() throws ImageFormatException, IOException {
		if (this.srcImageWidth >= this.srcImageHeight)
			this.destImageHeight = (int) Math.round(this.srcImageHeight
					* this.destImageWidth * 1.0D / this.srcImageWidth);
		else {
			this.destImageWidth = (int) Math.round(this.srcImageWidth
					* this.destImageHeight * 1.0D / this.srcImageHeight);
		}
		BufferedImage _image = new BufferedImage(this.destImageWidth,
				this.destImageHeight, 1);
		_image.getGraphics().drawImage(this.image, 0, 0, this.destImageWidth,
				this.destImageHeight, null);
		FileOutputStream out = new FileOutputStream(this.destImage);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(_image);
		out.flush();
		out.close();
	}

	public void resize(int w, int h) throws ImageFormatException, IOException {
		if (this.srcImageWidth >= this.srcImageHeight)
			h = (int) Math.round(this.srcImageHeight * w * 1.0D
					/ this.srcImageWidth);
		else {
			w = (int) Math.round(this.srcImageWidth * h * 1.0D
					/ this.srcImageHeight);
		}
		BufferedImage _image = new BufferedImage(w, h, 1);
		_image.getGraphics().drawImage(this.image, 0, 0, w, h, null);
		FileOutputStream out = new FileOutputStream(this.destImage);
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(_image);
		out.flush();
		out.close();
	}

	public static void main(String[] args) throws ImageFormatException,
			IOException {
		String srcImage = "E:/ent/照片/姐姐/婚纱照/DSC_5953.JPG";

		System.out.println(srcImage.indexOf("E:"));
		System.out.println(srcImage.substring(srcImage.indexOf("E:")
				+ "E:".length()) + 1);
	}
}